/**

 * User: seeker910
 * Date: 13-11-8
 * Time: 上午1:36

 */
Rsd.define('Rsd.form.Text', {
    extend: 'Rsd.form.Input',
    xtype: 'text',
    margin:'2 0 2 0',
//    height: 25,
//    width: 80,
    inputType: 'text',
    ctrlCls: 'x-control-text',
    maxlength:null,
    //属性规定了一个表单控件的值应该匹配的正则表达式
    pattern:null,
    handler: null,
    /**
     * @description 输入框选择器配置
     * {
        style:null,
        text:'[选 择]',
        handler:'' //优先使用
        viewerType:''
       }
     */
     //selector:null,
     /**
      * 
      * @param {*} config 
      */
    constructor: function constructor(config) {
        config = config || {};
        this.apply(config);
    },
    /**
     * 初始化 仅创建对象，并未渲染dom结构
     */
     initComponentEx: function initComponentEx() 
     {
            this.callParent();
            var me = this;
            if(me.maxlength != undefined && me.maxlength != null)
            {
                me.ctrl.element.setAttribute("maxlength",this.maxlength);
            }
            if(me.pattern != undefined && me.pattern != null)
            {
                //属性规定了一个表单控件的值应该匹配的正则表达式
                me.ctrl.element.setAttribute("pattern",this.pattern);
            }
            if(this.selector && this.readonly !== true)
            {
                var me = this;
                var _selector = me.selector;
                var btn = document.createElement('a');
                _selector.element = btn;
                
                btn.id = this.id + '__selector';
                btn.innerHTML = _selector.text || Rsd.lang('form.text.selector.chooseButtonText');
                btn.title = _selector.tip||_selector.title||'';
                btn.style.marginRight = "8px";
                btn.style.marginLeft = "10px";
                btn.style.float = "right";
                btn.style.position = "relative";
                btn.style.zIndex = 9999;
                btn.style.lineHeight = me.height + 'px';
                btn.href="javascript:void(0);"

                me.ctrl.element.ondblclick = btn.onclick =  function (event) {
                    if(_selector.handler||_selector.fn)
                    {
                        me.funApplyByIOC(_selector.handler||_selector.fn, [me, event]);
                        return;
                    } 
                    if(_selector.viewerType)
                    {
                        var _where = _selector.filter||[]; 
                        if(Rsd.isObject(_where))
                        {
                            //console.log(_where.value,me.parent.parent.getRecord()); 
                            var _data = me.parent.parent.data||{};
                            if(me.parent.parent.getRecord)
                            {
                                _data = me.parent.parent.getRecord();
                            }
                            //格式化value
                            _where = [{
                                name:_where.name,
                                op:_where.op,
                                value: Rsd.template(_where.value,_data)
                            }];
                        }
                        Rsd.create(_selector.viewerType,{
                            readonly:true,
                            autoLoad:true,
                            title:_selector.title || ('请选择-' + me.label.content),  
                            modelType:_selector.modelType, 
                            chooseColumn:{
                                xtype:'checkbox',
                                text:Rsd.lang('form.text.selector.chooseColumnText'),
                                width:60,
                                fixed:true,
                                multiple:_selector.multiple===true,
                                align:'center',
                                keyMember:_selector.valueMember,
                                dataIndex:'_sel_'+_selector.valueMember,
                                editable:true, 
                            },
                            where:_where,
                            sort:_selector.sort||[],
                            gridColumns:_selector.gridColumns||[],
                            width:_selector.width||800,
                            height:_selector.height||600,
                            newButtons:_selector.newButtons,
                            buttons: [
                                {
                                    text:Rsd.lang('form.text.selector.okText'),
                                    width:120,
                                    handler:function(rows,btn){
                                        for(var i in rows)
                                        {
                                            var row = rows[i];
                                            if(row['_sel_' + me.selector.valueMember])
                                            { 
                                                if(me.selector.multiple)
                                                {
                                                   //暂未实现多选
                                                   console.error('暂未实现多选');
                                                }
                                                else
                                                { 
                                                    me.setValue(row[me.selector.valueMember]);
                                                    me.ctrl.element.value = row[me.selector.valueMember]||'-';
                                                    break
                                                }
                                            }
                                        }
                                        //console.log(me.getValue());
                                        btn.parent.close();
                                    }
                                }
                            ],
                            parent:me,
                            footBar:_selector.footBar||{xtype:'container',height:40,width:'100%',border:true},
                            showItem:function(){}
                
                        }).showDialog();
                        return;
                    }

                    alert('selector没有设置handler或viewerType');
                    return;
                }

                me.setElStyle(btn,_selector.style||{});

                var btnClear = document.createElement('a');
                _selector.clearElement = btnClear;
                 
                btnClear.id = this.id + '__clear';
                btnClear.innerHTML = _selector.clearText || Rsd.lang('form.text.selector.clearButtonText');
                btnClear.title = _selector.clearTip||_selector.clearTitle||'';
                btnClear.style.marginRight = "8px";
                btnClear.style.marginLeft = "0px";
                btnClear.style.float = "right";
                btnClear.style.position = "relative";
                btnClear.style.zIndex = 9999;
                btnClear.style.lineHeight = me.height + 'px';
                btnClear.href="javascript:void(0);"
                btnClear.onclick = function (event)
                {
                    if(_selector.clearHandler||_selector.clearFn)
                    {
                        me.funApplyByIOC(_selector.clearHandler||_selector.clearFn, [me, event]);
                    }
 
                    me.setValue("");
                }
            } 
     },
     /**
      * 
      */
     onAfterRender:function onAfterRender()
     {
        this.callParent();

        if(this.selector && this.selector.element)
        { 
            this.dom.appendChild(this.selector.element);
        }

        if(this.selector && this.selector.clearElement)
        { 
            this.dom.appendChild(this.selector.clearElement);
        }
     }

},function(type){

    var _selectorGetter = function () {

        return this.___selector;
    };

    var _selectorSetter = function (value) {
        
        if(Rsd.isEmpty(value))
        {
            this.___selector = null;
            return;
        }
        this.___selector = Rsd.apply({style:null, text:Rsd.lang('form.text.selector.chooseButtonText'), handler:''},value); 
    }

    this.defineProperty(type,"selector", _selectorGetter, _selectorSetter,false);
});

